package com.molichuxing.services.infrastructure.convertor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.business.dto.request.create.OfflineBizCreateDto;
import com.molichuxing.services.business.dto.request.create.OfflineOrdersBizCreateDto;
import com.molichuxing.services.business.dto.request.create.PaymentBizCreateDto;
import com.molichuxing.services.business.dto.request.create.PaymentDetailBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.AioPayResultBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.OfflineAuditBizModifyDto;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.infrastructure.bean.*;
import com.molichuxing.services.infrastructure.dto.request.create.*;
import com.molichuxing.services.infrastructure.dto.request.modify.AioPayResultModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OfflineAuditModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.PaymentModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.property.*;
import com.molichuxing.services.property.sdk.AlipayTradeStateEnum;
import com.molichuxing.services.property.sdk.WechatpayTradeStateEnum;
import com.molichuxing.services.property.sdk.WechatpayTradeTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhaopenghui
 * @create_date: 2019-08-21
 **/
public class Convert {


    /*********************************线下支付转换开始*************************************************/

    public static OfflineBean toOfflineBean(OfflineCreateDto offlineCreateDto) {
        OfflineBean offlineBean = new OfflineBean();
        if (offlineCreateDto != null) {
            BeanUtils.copyProperties(offlineCreateDto, offlineBean);
        }
        return offlineBean;
    }

    public static List<OfflineOrdersBean> toOfflineOrdersBeanList(List<OfflineOrdersCreateDto> offlineOrdersList) {
        List<OfflineOrdersBean> beanList = new ArrayList<>();
        if (offlineOrdersList != null && offlineOrdersList.size() > 0) {
            for (OfflineOrdersCreateDto offlineOrdersCreateDto : offlineOrdersList) {
                beanList.add(toOfflineOrdersBean(offlineOrdersCreateDto));
            }
        }
        return beanList;
    }

    public static OfflineOrdersBean toOfflineOrdersBean(OfflineOrdersCreateDto offlineOrdersCreateDto) {
        OfflineOrdersBean offlineOrdersBean = new OfflineOrdersBean();
        if (offlineOrdersCreateDto != null) {
            BeanUtils.copyProperties(offlineOrdersCreateDto, offlineOrdersBean);
        }
        return offlineOrdersBean;
    }

    public static OfflineCreateDto toOfflineCreateDto(OfflineBizCreateDto offlineBizCreateDto) {
        OfflineCreateDto offlineCreateDto = new OfflineCreateDto();
        if (offlineBizCreateDto != null) {
            BeanUtils.copyProperties(offlineBizCreateDto, offlineCreateDto);
            List<String> voucherImgList = offlineBizCreateDto.getVoucherImgList();
            offlineCreateDto.setVoucherImg(JSONObject.toJSONString(voucherImgList));
        }
        return offlineCreateDto;
    }

    public static List<OfflineOrdersCreateDto> toOfflineOrdersList(Integer offlineId, List<OfflineOrdersBizCreateDto> offlineOrdersBizList) {
        List<OfflineOrdersCreateDto> offlineOrdersList = new ArrayList<>();
        if (offlineOrdersBizList != null && offlineOrdersBizList.size() > 0) {
            for (OfflineOrdersBizCreateDto offlineOrdersBizCreateDto : offlineOrdersBizList) {
                offlineOrdersList.add(toOfflineOrdersCreateDto(offlineId, offlineOrdersBizCreateDto));
            }
        }
        return offlineOrdersList;
    }

    public static OfflineOrdersCreateDto toOfflineOrdersCreateDto(Integer offlineId, OfflineOrdersBizCreateDto offlineOrdersBizCreateDto) {
        OfflineOrdersCreateDto offlineOrdersCreateDto = new OfflineOrdersCreateDto();
        if (offlineOrdersBizCreateDto != null) {
            BeanUtils.copyProperties(offlineOrdersBizCreateDto, offlineOrdersCreateDto);
            offlineOrdersCreateDto.setOfflineId(offlineId);
        }
        return offlineOrdersCreateDto;
    }

    public static OfflineAuditDto toOfflineAuditDto(OfflineBean offlineBean) {
        OfflineAuditDto offlineAuditDto = new OfflineAuditDto();
        if (offlineBean != null) {
            BeanUtils.copyProperties(offlineBean, offlineAuditDto);
            offlineAuditDto.setOfflineId(offlineBean.getId());
            Integer auditStatus = offlineBean.getAuditStatus();
            if (auditStatus != null) {
                offlineAuditDto.setAuditStatusStr(OfflineAuditStatusEnum.getName(auditStatus));
            }
        }
        return offlineAuditDto;
    }

    public static OfflineAuditInfoBizDto toOfflineAuditInfoBizDto(OfflineAuditDto offlineAuditDto) {
        OfflineAuditInfoBizDto offlineAuditInfoBizDto = new OfflineAuditInfoBizDto();
        if (offlineAuditDto != null) {
            BeanUtils.copyProperties(offlineAuditDto, offlineAuditInfoBizDto);
        }
        return offlineAuditInfoBizDto;
    }

    public static List<OfflineDto> toOfflineDtoList(List<OfflineBean> beanList) {
        List<OfflineDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (OfflineBean offline : beanList) {
                dtoList.add(toOfflineDto(offline));
            }
        }
        return dtoList;
    }

    public static OfflineDto toOfflineDto(OfflineBean bean) {
        OfflineDto beanDto = new OfflineDto();
        if (bean != null) {
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setOfflineId(bean.getId());
            if (bean.getVoucherImg() != null) {
                beanDto.setVoucherImgs(JSONObject.parseArray(bean.getVoucherImg(), String.class));
            }
        }
        return beanDto;
    }

    public static OfflineOrdersDto toOfflineOrdersDto(OfflineOrdersBean bean) {
        OfflineOrdersDto offlineOrdersDto = new OfflineOrdersDto();
        if (bean != null) {
            BeanUtils.copyProperties(bean, offlineOrdersDto);
        }
        return offlineOrdersDto;
    }

    public static OfflineBizDto toOfflineBizDto(OfflineDto offlineDto) {
        OfflineBizDto offlineBizDto = new OfflineBizDto();
        if (offlineDto != null) {
            BeanUtils.copyProperties(offlineDto, offlineBizDto);
        }
        return offlineBizDto;
    }

    public static OfflineBean toOfflineBean(OfflineAuditModifyDto offlineAuditModifyDto) {
        OfflineBean offlineBean = new OfflineBean();
        if (offlineAuditModifyDto != null) {
            BeanUtils.copyProperties(offlineAuditModifyDto, offlineBean);
            offlineBean.setId(offlineAuditModifyDto.getOfflineId());
            offlineBean.setAuditId(offlineAuditModifyDto.getOperateId());
            offlineBean.setAuditName(offlineAuditModifyDto.getOperateName());
            offlineBean.setAuditTime(offlineAuditModifyDto.getOperateDate());
        }
        return offlineBean;
    }

    public static OfflineAuditModifyDto toOfflineAuditModifyDto(OfflineAuditBizModifyDto offlineAuditBizModifyDto) {
        OfflineAuditModifyDto offlineAuditModifyDto = new OfflineAuditModifyDto();
        if (offlineAuditBizModifyDto != null) {
            BeanUtils.copyProperties(offlineAuditBizModifyDto, offlineAuditModifyDto);
        }
        return offlineAuditModifyDto;
    }

    public static List<OfflineOrdersDto> toOfflineOrdersList(List<OfflineOrdersBean> offlineOrdersBeanList) {
        List<OfflineOrdersDto> beanList = new ArrayList<>();
        if (offlineOrdersBeanList != null && offlineOrdersBeanList.size() > 0) {
            for (OfflineOrdersBean offlineOrdersBean : offlineOrdersBeanList) {
                beanList.add(toOfflineOrdersDto(offlineOrdersBean));
            }
        }
        return beanList;
    }

    /*********************************线下支付转换结束*************************************************/


    /*********************************支付转换开始*************************************************/

    public static PaymentBean toPaymentBean(PaymentCreateDto createDto) {
        PaymentBean bean = new PaymentBean();
        if (createDto != null) {
            BeanUtils.copyProperties(createDto, bean);
            bean.setPayWay(createDto.getPayWay() == null ? null : createDto.getPayWay().getValue());
            bean.setPayStatus(createDto.getPayStatus().getValue());
            bean.setBusinessType(createDto.getBusinessType().getValue());
            bean.setCostType(createDto.getCostType().getValue());
        }
        return bean;
    }

    public static List<PaymentDetailBean> toPaymentDetailBean(List<PaymentDetailCreateDto> paymentDetails) {
        List<PaymentDetailBean> paymentDetailBeanList = new ArrayList<>();
        if (paymentDetails != null && paymentDetails.size() > 0) {
            for (PaymentDetailCreateDto paymentDetailCreateDto : paymentDetails) {
                paymentDetailBeanList.add(toPaymentDetailBean(paymentDetailCreateDto));
            }
        }
        return paymentDetailBeanList;
    }

    public static PaymentDetailBean toPaymentDetailBean(PaymentDetailCreateDto paymentDetailCreateDto) {
        PaymentDetailBean paymentDetailBean = new PaymentDetailBean();
        if (paymentDetailCreateDto != null) {
            BeanUtils.copyProperties(paymentDetailCreateDto, paymentDetailBean);
            paymentDetailBean.setType(paymentDetailCreateDto.getType().getValue());
        }
        return paymentDetailBean;
    }

    public static List<PaymentDto> toPaymentDto(List<PaymentBean> beanList) {
        List<PaymentDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (PaymentBean paymentBean : beanList) {
                dtoList.add(toPaymentDto(paymentBean));
            }
        }
        return dtoList;
    }

    public static PaymentDto toPaymentDto(PaymentBean paymentBean) {
        PaymentDto paymentDto = null;
        if (paymentBean != null) {
            paymentDto = new PaymentDto();
            BeanUtils.copyProperties(paymentBean, paymentDto);
        }
        return paymentDto;
    }


    public static List<PaymentBizDto> toPaymentBizDto(List<PaymentDto> beanDtoList) {
        List<PaymentBizDto> bizDtoList = new ArrayList<>();
        if (beanDtoList != null && beanDtoList.size() > 0) {
            for (PaymentDto beanDto : beanDtoList) {
                bizDtoList.add(toPaymentBizDto(beanDto));
            }
        }
        return bizDtoList;
    }

    public static PaymentBizDto toPaymentBizDto(PaymentDto beanDto) {
        PaymentBizDto bizDto = new PaymentBizDto();
        if (beanDto != null) {
            BeanUtils.copyProperties(beanDto, bizDto);
            bizDto.setPaymentId(beanDto.getId());
            if (PaymentCostTypeEnum.RENEW.equals(beanDto.getCostType())) {
                bizDto.setCostType(PaymentCostTypeEnum.SUBSCRIBE);
            }
        }
        return bizDto;
    }

    public static PaymentDownloadBizDto toPaymentDownloadBizDto(PaymentDto beanDto) {
        PaymentDownloadBizDto bizDto = new PaymentDownloadBizDto();
        if (beanDto != null) {
            BeanUtils.copyProperties(beanDto, bizDto);
            bizDto.setPaymentId(beanDto.getId());
            if (PaymentCostTypeEnum.RENEW.equals(beanDto.getCostType())) {
                bizDto.setCostType(PaymentCostTypeEnum.SUBSCRIBE);
            }
        }
        return bizDto;
    }

    public static List<PaymentDetailDto> toPaymentDetailDto(List<PaymentDetailBean> paymentDetailBeanList) {
        List<PaymentDetailDto> dtoList = new ArrayList<>();
        if (paymentDetailBeanList != null && paymentDetailBeanList.size() > 0) {
            for (PaymentDetailBean paymentDetailBean : paymentDetailBeanList) {
                dtoList.add(toPaymentDetailDto(paymentDetailBean));
            }
        }
        return dtoList;
    }


    public static PaymentDetailDto toPaymentDetailDto(PaymentDetailBean paymentDetailBean) {
        PaymentDetailDto paymentDetailDto = new PaymentDetailDto();
        if (paymentDetailBean != null) {
            BeanUtils.copyProperties(paymentDetailBean, paymentDetailDto);
        }
        return paymentDetailDto;
    }

    public static List<PaymentRecordBizDto> toPaymentRecordBizDto(List<PaymentDto> beanDtoList) {
        List<PaymentRecordBizDto> beanBizDtoList = null;
        if (beanDtoList != null && !beanDtoList.isEmpty()) {
            beanBizDtoList = new ArrayList<>();
            for (PaymentDto beanDto : beanDtoList) {
                beanBizDtoList.add(toPaymentRecordBizDto(beanDto));
            }
        }
        return beanBizDtoList;
    }

    public static PaymentRecordBizDto toPaymentRecordBizDto(PaymentDto beanDto) {
        PaymentRecordBizDto beanBizDto = null;
        if (beanDto != null) {
            beanBizDto = new PaymentRecordBizDto();
            BeanUtils.copyProperties(beanDto, beanBizDto);
        }
        return beanBizDto;
    }

    public static List<PaymentDetailBizDto> toPaymentDetailBizDto(List<PaymentDetailDto> beanDtoList) {
        List<PaymentDetailBizDto> beanBizDtoList = null;
        if (beanDtoList != null && !beanDtoList.isEmpty()) {
            beanBizDtoList = new ArrayList<>();
            for (PaymentDetailDto beanDto : beanDtoList) {
                beanBizDtoList.add(toPaymentDetailBizDto(beanDto));
            }
        }
        return beanBizDtoList;
    }

    public static PaymentDetailBizDto toPaymentDetailBizDto(PaymentDetailDto beanDto) {
        PaymentDetailBizDto beanBizDto = null;
        if (beanDto != null) {
            beanBizDto = new PaymentDetailBizDto();
            BeanUtils.copyProperties(beanDto, beanBizDto);
        }
        return beanBizDto;
    }

    /********************************* 支付转换结束 *************************************************/

    public static WechatpayTradeTypeEnum toWechatpayTradeTypeEnum(PaymentPayWayEnum payWay) {
        WechatpayTradeTypeEnum result = null;
        switch (payWay) {
            case WXPAY_MWEB:
                result = WechatpayTradeTypeEnum.MWEB;
                break;
            case WXPAY_APP:
                result = WechatpayTradeTypeEnum.APP;
                break;
            case WXPAY_JSAPI:
                result = WechatpayTradeTypeEnum.JSAPI;
                break;
            case WXPAY_NATIVE:
                result = WechatpayTradeTypeEnum.NATIVE;
                break;
            case WXPAY_APPLET:
                result = WechatpayTradeTypeEnum.APPLET;
                break;
            default:
                break;
        }

        return result;
    }

    public static WechatpayCreateDto toWechatpayCreateDto(PaymentBizCreateDto bizCreateDto)
            throws UnsupportedEncodingException {
        WechatpayCreateDto createDto = null;
        if (bizCreateDto != null) {
            createDto = new WechatpayCreateDto();
            // 支付场景 (APP:APP支付, MWEB:H5支付, JSAPI:公众号支付, NATIVE:扫码支付)
            createDto.setTradeType(toWechatpayTradeTypeEnum(bizCreateDto.getPayWay()));
            // 商品ID(订单号)
            createDto.setProductId(bizCreateDto.getOrderCode().toString());
            // 订单金额(元)
            createDto.setTotalFee(bizCreateDto.getPayAmount());
            // 微信用户openid(公众号支付必传)
            createDto.setOpenid(bizCreateDto.getOpenid());
            // 支付终端ip(公众号、H5、APP支付必传)
            createDto.setIp(bizCreateDto.getDeviceInfo());
            // 商品描述
            createDto.setBody(bizCreateDto.getRemark());
            // 附加数据
            createDto.setAttach(createDto.getTradeType().toString());
            // 是否禁用信用卡支付(true:禁用)
            createDto.setIsCredit(true);
            // 交易起始时间
            createDto.setTimeStart(bizCreateDto.getTimeStart());
            // 交易结束时间
            createDto.setTimeExpire(bizCreateDto.getTimeEnd());
        }

        return createDto;
    }

    public static PaymentCreateDto toPaymentCreateDto(PaymentBizCreateDto bizCreateDto, PrepaymentDto prepaymentDto) {
        PaymentCreateDto createDto = new PaymentCreateDto();
        BeanUtils.copyProperties(bizCreateDto, createDto);
        // 支付单号
        createDto.setPayNumber(prepaymentDto.getOutTradeNo());
        // 预支付数据
        createDto.setRequestData(prepaymentDto.getRequestData());
        createDto.setPayStatus(PaymentStatusEnum.NO_PAYMENT);

        return createDto;
    }

    public static PaymentModifyDto toPaymentModifyDto(PaymentBizCreateDto createBizDto, PrepaymentDto prepaymentDto) {
        PaymentModifyDto modifyDto = new PaymentModifyDto();
        BeanUtils.copyProperties(createBizDto, modifyDto);
        // 支付单号
        modifyDto.setPayNumber(prepaymentDto.getOutTradeNo());
        // 预支付数据
        modifyDto.setRequestData(prepaymentDto.getRequestData());
        modifyDto.setDeviceInfo(createBizDto.getDeviceInfo());

        return modifyDto;
    }

    public static PaymentBean toPaymentBean(PaymentModifyDto modifyDto) {
        PaymentBean bean = new PaymentBean();
        if (modifyDto != null) {
            BeanUtils.copyProperties(modifyDto, bean);
            bean.setPayWay(modifyDto.getPayWay() == null ? null : modifyDto.getPayWay().getValue());
            bean.setPayStatus(modifyDto.getPayStatus().getValue());
        }
        return bean;
    }

    public static NotifyBizDto toNotifyBizDto(WechatpayResultDto beanDto) {
        NotifyBizDto beanBizDto = new NotifyBizDto();
        beanBizDto.setPayAmount(beanDto.getTotalFee());
        beanBizDto.setPayNumber(beanDto.getOutTradeNo());
        beanBizDto.setPayTime(beanDto.getTimeEnd());
        beanBizDto.setTransactionId(beanDto.getTransactionId());

        beanBizDto.setPayResult(toPaymentResultEnum(beanDto.getTradeState()));
        return beanBizDto;
    }

    public static NotifyBizDto toNotifyBizDto(UnionpayResultDto beanDto) {
        NotifyBizDto beanBizDto = new NotifyBizDto();
        beanBizDto.setPayAmount(beanDto.getTotalAmount());
        beanBizDto.setPayNumber(beanDto.getOutTradeNo());
        beanBizDto.setPayTime(beanDto.getGmtPayment());
        beanBizDto.setTransactionId(beanDto.getTradeNo());

        beanBizDto.setPayResult(toPaymentResultEnum(beanDto.getTradeState()));
        return beanBizDto;
    }


    public static PaymentResultEnum toPaymentResultEnum(WechatpayTradeStateEnum wechatpayTradeState) {
        PaymentResultEnum result = null;
        switch (wechatpayTradeState) {
            case SUCCESS:
                result = PaymentResultEnum.SUCCESS;
                break;
            case NOTPAY:
                result = PaymentResultEnum.NOTPAY;
                break;
            default:
                result = PaymentResultEnum.PAYERROR;
        }
        return result;
    }

    public static PaymentModifyDto toPaymentModifyDto(PaymentDto paymentDto, NotifyBizDto result) {
        PaymentModifyDto modifyDto = new PaymentModifyDto();
        BeanUtils.copyProperties(paymentDto, modifyDto);
        modifyDto.setThirdPayNumber(result.getTransactionId());
        modifyDto.setPayTime(result.getPayTime());
        modifyDto.setPayWay(paymentDto.getPayWay());
        switch (result.getPayResult()) {
            case SUCCESS:
                modifyDto.setPayStatus(PaymentStatusEnum.SUCCESS_PAYMENT);
                break;
            case NOTPAY:
                modifyDto.setPayStatus(PaymentStatusEnum.NO_PAYMENT);
                break;
            default:
                modifyDto.setPayStatus(PaymentStatusEnum.FAIL_PAYMENT);
        }

        return modifyDto;
    }

    public static List<PaymentDetailCreateDto> toPaymentDetailCreateDto(List<PaymentDetailBizCreateDto> createBizDtos,
                                                                        Integer paymentId, Long orderCode) {
        List<PaymentDetailCreateDto> createDtos = null;
        if (createBizDtos != null && !createBizDtos.isEmpty()) {
            createDtos = new ArrayList<>();
            for (PaymentDetailBizCreateDto createBizDto : createBizDtos) {
                createDtos.add(toPaymentDetailCreateDto(createBizDto, paymentId, orderCode));
            }
        }
        return createDtos;
    }

    public static PaymentDetailCreateDto toPaymentDetailCreateDto(PaymentDetailBizCreateDto createBizDto,
                                                                  Integer paymentId, Long orderCode) {
        PaymentDetailCreateDto createDto = null;
        if (createBizDto != null) {
            createDto = new PaymentDetailCreateDto();
            BeanUtils.copyProperties(createBizDto, createDto);
            createDto.setPaymentId(paymentId);
            createDto.setOrderCode(orderCode);
        }

        return createDto;
    }

    public static NotifyBizDto toNotifyBizDto(PaymentDto paymentDto) {
        NotifyBizDto result = new NotifyBizDto();
        result.setOrderCode(paymentDto.getOrderCode());
        result.setBusinessType(paymentDto.getBusinessType());
        result.setCostType(paymentDto.getCostType());
        result.setUserId(paymentDto.getUserId());
        result.setPayWay(paymentDto.getPayWay());
        result.setScheduleId(paymentDto.getScheduleId());
        result.setPayAmount(paymentDto.getPayAmount());
        result.setPayNumber(paymentDto.getPayNumber());
        result.setPayTime(paymentDto.getPayTime());
        result.setTransactionId(paymentDto.getThirdPayNumber());
        return result;
    }

    public static PaymentHistoryBean toPaymentHistoryBean(PaymentHistoryCreateDto createDto) {
        PaymentHistoryBean bean = null;
        if (createDto != null) {
            bean = new PaymentHistoryBean();
            BeanUtils.copyProperties(createDto, bean);
            bean.setPayWay(createDto.getPayWay() == null ? null : createDto.getPayWay().getValue());
            bean.setBusinessType(createDto.getBusinessType().getValue());
            bean.setCostType(createDto.getCostType().getValue());
        }
        return bean;
    }

    public static PaymentHistoryCreateDto toPaymentHistoryCreateDto(PaymentDto beanDto) {
        PaymentHistoryCreateDto createDto = null;
        if (beanDto != null) {
            createDto = new PaymentHistoryCreateDto();
            BeanUtils.copyProperties(beanDto, createDto);
            createDto.setPaymentId(beanDto.getId());
        }
        return createDto;
    }

    public static AioPayResultModifyDto toAioPayResultModifyDto(AioPayResultBizModifyDto bizModifyDto,
                                                                PaymentDto beanDto) {
        AioPayResultModifyDto modifyDto = new AioPayResultModifyDto();
        BeanUtils.copyProperties(bizModifyDto, modifyDto);
        modifyDto.setPaymentId(beanDto.getId());
        return modifyDto;
    }

    public static NotifyBizDto toNotifyBizDto(AioPayResultBizModifyDto modifyDto) {
        NotifyBizDto beanBizDto = new NotifyBizDto();
        beanBizDto.setPayAmount(modifyDto.getPayAmount());
        beanBizDto.setPayNumber(modifyDto.getPayNumber());
        beanBizDto.setPayTime(modifyDto.getPayTime());
        beanBizDto.setTransactionId(modifyDto.getThirdPayNumber());
        beanBizDto.setPayResult(modifyDto.getIsSuccess() ? PaymentResultEnum.SUCCESS : PaymentResultEnum.PAYERROR);

        return beanBizDto;
    }

    public static NotifyBizDto toNotifyBizDto(AlipayResultDto beanDto) {
        NotifyBizDto beanBizDto = new NotifyBizDto();
        beanBizDto.setPayAmount(beanDto.getTotalAmount());
        beanBizDto.setPayNumber(beanDto.getOutTradeNo());
        beanBizDto.setPayTime(beanDto.getGmtPayment());
        beanBizDto.setTransactionId(beanDto.getTradeNo());

        beanBizDto.setPayResult(toPaymentResultEnum(beanDto.getTradeState()));
        return beanBizDto;
    }

    public static PaymentResultEnum toPaymentResultEnum(AlipayTradeStateEnum tradeState) {
        PaymentResultEnum result = null;
        switch (tradeState) {
            case TRADE_SUCCESS:
                result = PaymentResultEnum.SUCCESS;
                break;
            case TRADE_CLOSED:
                result = PaymentResultEnum.NOTPAY;
                break;
            case WAIT_BUYER_PAY:
                result = PaymentResultEnum.NOTPAY;
                break;
            case TRADE_FINISHED:
                result = PaymentResultEnum.FINISHED;
                break;
            default:
                result = PaymentResultEnum.PAYERROR;
        }
        return result;
    }

    public static AlipayCreateDto toAlipayCreateDto(PaymentBizCreateDto bizCreateDto) {
        AlipayCreateDto createDto = null;
        if (bizCreateDto == null) {
            return createDto;
        }
        createDto = new AlipayCreateDto();
        // 商户原始订单号(订单号)
        createDto.setMerchantOrderNo(bizCreateDto.getOrderCode() + "");
        // 订单金额(元)
        createDto.setTotalAmount(bizCreateDto.getPayAmount());
        // 商品的标题
        createDto.setSubject(bizCreateDto.getRemark());
        // 商品描述
        createDto.setBody(bizCreateDto.getRemark());
        // 附加数据
        // createDto.setPassbackParams(passbackParams);
        // 交易起始时间
        createDto.setTimeStart(bizCreateDto.getTimeStart());
        // 交易结束时间
        createDto.setTimeExpire(bizCreateDto.getTimeEnd());

        return createDto;
    }

    public static OfflineBookyueBean toOfflineBookyueBean(OfflineBookyueCreateDto createDto) {
        OfflineBookyueBean bean = null;
        if (createDto == null) {
            return bean;
        }
        bean = new OfflineBookyueBean();
        BeanUtils.copyProperties(createDto, bean);

        return bean;
    }

    public static OfflineBookyueDto toOfflineBookyueDto(OfflineBookyueBean bean) {
        OfflineBookyueDto dto = null;
        if (bean == null) {
            return dto;
        }
        dto = new OfflineBookyueDto();
        BeanUtils.copyProperties(bean, dto);
        String voucher = bean.getVoucher();
        if (StringUtils.isNoneBlank(voucher)) {
            JSONArray parseArray = JSONObject.parseArray(voucher);
            dto.setVoucher(parseArray.toJavaList(String.class));
        }


        return dto;
    }

    public static List<EscrowLogDto> toEscrowLogDto(List<EscrowLogBean> beanList) {
        List<EscrowLogDto> beanDtoList = null;
        if (beanList != null && beanList.size() > 0) {
            beanDtoList = new ArrayList<>();
            for (EscrowLogBean bean : beanList) {
                beanDtoList.add(toEscrowLogDto(bean));
            }
        }

        return beanDtoList;
    }

    private static EscrowLogDto toEscrowLogDto(EscrowLogBean bean) {
        EscrowLogDto beanDto = null;
        if (bean != null) {
            // 判断渠道
            if (bean.getChannel() == PaymentChannelEnum.ALIPAY.getValue()) {
                // 支付宝
                AlipayLogDto alipayLogDto = new AlipayLogDto();
                BeanUtils.copyProperties(bean, alipayLogDto);
                Map<String, Object> alipayTradeAppPayRequest = JSONObject.parseObject(bean.getRequestData(), Map.class);
                alipayTradeAppPayRequest = alipayTradeAppPayRequest == null ? new HashMap() : alipayTradeAppPayRequest;
                AlipayTradeAppPayModel alipayTradeAppPayModel = JSONObject.parseObject(ObjectCastUtil.castString(alipayTradeAppPayRequest.get("bizModel")), AlipayTradeAppPayModel.class);
                alipayLogDto.setBody(alipayTradeAppPayModel.getBody());
                alipayLogDto.setOutTradeNo(alipayTradeAppPayModel.getOutTradeNo());
                alipayLogDto.setProductCode(alipayTradeAppPayModel.getProductCode());
                alipayLogDto.setTotalAmount(alipayTradeAppPayModel.getTotalAmount());
                alipayLogDto.setApiVersion(ObjectCastUtil.castString(alipayTradeAppPayRequest.get("apiVersion")));
                alipayLogDto.setApiMethodName(ObjectCastUtil.castString(alipayTradeAppPayRequest.get("apiMethodName")));

                beanDto = alipayLogDto;
            } else if (bean.getChannel() == PaymentChannelEnum.WXPAY.getValue()) {
                // 微信
                beanDto = JSONObject.parseObject(bean.getRequestData(), WechatpayLogDto.class);
                BeanUtils.copyProperties(bean, beanDto);
            } else {
                // 其他类型
                beanDto = new EscrowLogDto();
                BeanUtils.copyProperties(bean, beanDto);
            }
        }

        return beanDto;
    }

    public static List<PaymentEnterpriseDto> toEnterpriseDtoList(List<EnterpriseBean> beanList) {
        List<PaymentEnterpriseDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (EnterpriseBean bean : beanList) {
                dtoList.add(toEnterpriseDto(bean));
            }
        }
        return dtoList;
    }

    public static PaymentEnterpriseDto toEnterpriseDto(EnterpriseBean bean) {
        PaymentEnterpriseDto beanDto = null;
        if (bean != null) {
            beanDto = new PaymentEnterpriseDto();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setPaymentEnterpriseId(bean.getId());
            beanDto.setVoucherImgs(JSONArray.parseArray(bean.getVoucherImg(), String.class));
        }
        return beanDto;
    }

    public static EnterpriseBean toEnterpriseBean(PaymentEnterpriseCreateDto createDto) {
        if (createDto == null) {
            return null;
        }
        EnterpriseBean bean = new EnterpriseBean();
        BeanUtils.copyProperties(createDto, bean);
        bean.setVoucherImg(JSONObject.toJSONString(createDto.getVoucherImgs()));


        return bean;
    }
}
